<?php
namespace app\admin\controller;
use think\Db;
use app\common\controller\Backend;
class Mukuai extends Backend{
	public function lists(){
		$list=model('ModelManage')->paginate(10);
	    $this->assign('list',$list);
		return $this->view->fetch();
	}
	public function edits(){
		$id=input('get.id');
		$data=array();
		if($id){
          $data=model('ModelManage')->where('id', $id)->find();
		}
		$parents=model('ModelManage')->select();
		$this->assign('data',$data);
		$this->assign('parents',$parents);
		return $this->view->fetch();
	}

	public function save(){
    	$data=$this->request->post();
    	$model=model('ModelManage');
    	if(empty($data['id'])){
           $model->data($data);
    	   $re=$model->save();
    	}else{
    	   $re=$model->save($data,['id' => $data['id']]);
    	}
    	if($re!==false){
    		$this->success(__('编辑成功'), 'Mukuai/lists');
    	}else{
    		$this->success(__('编辑失败'), 'Mukuai/lists');
    	}
    } 


    public function delete(){
    	$id=input('get.id');
    	$re=model('ModelManage')->destroy($id);
        if($re){
    		$this->success(__('删除成功'), 'Mukuai/lists');
    	}else{
    		$this->success(__('删除失败'), 'Mukuai/lists');
    	}
    }



    //功能列表

	public function glist(){
        $where=array();
        input('get.model_id')?$where['model_id']=input('get.model_id'):'';

		$list=Db::table('member_gongnen')
        ->alias('a')
        ->join('member_gongnen_child b','a.id = b.gid','LEFT')
        ->where($where)
        ->field('a.id,a.name,a.model_id,a.status as status,b.id as cid,b.cname,b.gid,b.type_id,b.status as ch_status')
        ->paginate(12);
		/*dump($list);
		die();*/
	    $this->assign('list',$list);
		return $this->view->fetch();
	}
	public function gedit(){
		$id=input('get.id');
		$data=array();
		$child=array();
		if($id){
          $data=Db::table('member_gongnen')->where('id', $id)->find();
          $child=Db::table('member_gongnen_child')->where('gid',$id)->select();
		}
		$models=model('ModelManage')->select();
		$type=model('AppType')->select();
		$this->assign('data',$data);
		$this->assign('child',$child);
		$this->assign('models',$models);
		$this->assign('type',$type);
        $this->assign('model_id',input('get.model_id'));
		return $this->view->fetch();
	}


	public function gsave(){
    	$data=$this->request->post();
        $childs=json_decode( $data['child'],true);
        unset($data['child']);
    	$model=model('GongNen');
    	if(empty($data['id'])){
            Db::table('member_gongnen')->insert($data);
            $re = Db::table('member_gongnen')->getLastInsID();
            foreach ($childs as $vo){
                $vo['gid']=$re;
                $vo['status']=1;
                $vo["type_id"]=rtrim($vo["type_id"],',');
                $row=Db::table('member_gongnen_child')->insert($vo);
           }
    	}else{
    	    $re=$model->save($data,['id' => $data['id']]);
            foreach ($childs as $vo){
                $vo["type_id"]=rtrim($vo["type_id"],',');
                if(empty($vo['id'])){
                    $vo['gid']=$data['id'];
                    $row=Db::table('member_gongnen_child')->insert($vo);
                }else{
                    $row=Db::table('member_gongnen_child')->where('id', $vo['id'])->update($vo);
                }
            }
    	}
    	if($re!==false){
    		$this->success(__('编辑成功'), 'Mukuai/glist');
    	}else{
    		$this->success(__('编辑失败'), 'Mukuai/glist');
    	}
    } 


    public function gdelete(){
    	$id=input('get.id');
        $re=Db::table('member_gongnen_child')->where('id',$id)->delete();
        if($re){
    		$this->success(__('删除成功'), 'Mukuai/glist');
    	}else{
    		$this->success(__('删除失败'), 'Mukuai/glist');
    	}
    }

    public function gongdelete(){
        $id=input('get.id');
        $re=Db::table('member_gongnen')->where('id',$id)->delete();
        if($re){
            $this->success(__('删除成功'), 'Mukuai/glist');
        }else{
            $this->success(__('删除失败'), 'Mukuai/glist');
        }
    }


    public function changeGongnenStatus(){
        $id=input('get.id');
        $status=input('get.status');
        $re=Db::table('member_gongnen')->where('gid',$id)->setField('status',$status);
        if($re){
            $this->success(__('修改成功'), 'Mukuai/glist');
        }else{
            $this->success(__('修改失败'), 'Mukuai/glist');
        }
    }

      public function changeChildGongnenStatus(){
        $id=input('get.id');
        $status=input('get.status');
        $re=Db::table('member_gongnen_child')->where('gid',$id)->setField('status',$status);
        if($re){
            $this->success(__('修改成功'), 'Mukuai/glist');
        }else{
            $this->success(__('修改失败'), 'Mukuai/glist');
        }
    }






	public function jishu(){
	    $gid=input('get.gid');
      
        $gid?$where['b.id']=$gid:$where=array();


        $map['is_model']=1;

        //应用类型
        $app_type= Db::table('member_app_type')->select();
        foreach ($app_type as $key => &$value) {
            $map['type_id']=$value['id'];

            $value['gongnens']=[];
            $gongnen=Db::table('member_gongnen')
                ->alias('a')
                ->join('member_gongnen_child b','a.id = b.gid','LEFT')
                ->where('b.type_id','like',"%{$value['id']}%")
                ->where($where)
                ->field('a.id,a.name,a.model_id,a.status as status,b.id as cid,b.cname,b.gid,b.type_id,b.status as ch_status')
                ->select();

            if(is_array($gongnen)){
                foreach ($gongnen as $m => $n){
                    $map['gid']=$n['cid'];

                    $n['list']=Db::table('member_technology_select')->where($map)->find();

                    !empty($n['list'])?$n['list']['technology']=json_decode($n['list']['technology'],true):'';

                    $n['model']=Db::table('member_model_manage')->where('id',$n['model_id'])->find();
                    $value['gongnens'][]=$n;
                }
            }
            if(empty($value['gongnens'])){
                unset($app_type[$key]);
            }

        }

     /*   dump($app_type[0]);
        die();*/

	    //时间预算列表
        $budget_manage= Db::table('member_budget_manage')->select();

        //应用语言
        //只获取后台的功能
        $cate=Db::table('member_app_category')->where(array('name'=>'功能开发','develop_model'=>1))->find();
        $technology_language=Db::table('member_app_language')->where('cate_id',$cate['id'])->select();

	    $this->assign('app_type',$app_type);
    
        $this->assign('budget_manage',$budget_manage);
        $this->assign('technology_language',$technology_language);
		return $this->view->fetch();
	}


    public function jsave(){
        $datas=$this->request->post();
        $datas['arrs']=json_decode($datas['arrs'],true);
        foreach ($datas['arrs'] as $data){
            if(empty($data['id'])){
                $data['is_model']=1;
                $re=Db::table('member_technology_select')->insert($data);
            }else{
                $re=Db::table('member_technology_select')->where('id', $data['id'])->update($data);
            }
        }

        if($re!==false){
            $this->success(__('编辑成功'), 'Mukuai/glist');
        }else{
            $this->success(__('编辑失败'), 'Mukuai/glist');
        }
    }


}